<?xml version="1.0" encoding="UTF-8"?>

<!--

    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<html>
<head>
<meta charset="utf-8"/>
<title>CXF Log-Browser using WebSocket (PlainView)</title>  
</head>
<body>
<h2>CXF Log-Browser using WebSocket (PlainView)</h2>  
<p>The purpose of this page is simply show how the log-feed can be subscribed and the log records are pushed over the WebSocket.</p>
<div id="output">
  <div id="config" style="float: left; width: 600px">
    <strong>Log-Browser Endpoint URL:</strong><br />
    <input id="wsUri" size="72" style="width: 100%" value="ws://localhost:8181/cxf/samples/logbrowser"/>
    <br/>
    <button id="connect">Connect</button>
    <button id="disconnect">Disconnect</button>
    <br />
    <br />
    <button id="subscribe">Subscribe</button>
    <button id="unsubscribe">Unsubscribe</button>
    <br />
    <br />
    <input id="modeatom" type="checkbox" name="mode" value="atom"/>Atom<br/>
    <br />
    <br />
  </div>
  <div id="log" style="float: left; width: 90%; height: 500px">
    <strong>Log:</strong><br />
    <button id="clearLogBut" style="position: relative; top: 3px;">Clear log</button>
    <div id="consoleLog" style="margin-top: 5px; border: solid 1px #aaaaaa; overflow-y: scroll; height:100%"></div>
  </div>
  <div class="clearfix"></div>
</div>
<script language="javascript" type="text/javascript">
  var output;

  var connectBut = document.getElementById("connect");
  connectBut.onclick = doConnect;

  var disconnectBut = document.getElementById("disconnect");
  disconnectBut.onclick = doDisconnect;
    
  var subBut = document.getElementById("subscribe");
  subBut.onclick = doSub;

  var unsubBut = document.getElementById("unsubscribe");
  unsubBut.onclick = doUnsub;

  var modeatomCB = document.getElementById("modeatom");

  var consoleLog = document.getElementById("consoleLog");

  var clearLogBut = document.getElementById("clearLogBut");
  clearLogBut.onclick = clearLog;

  function init() {
    output = document.getElementById("output");
    setGuiConnected(false);
  }

  function doConnect() {
    var wsUri = document.getElementById("wsUri");

    websocket = new WebSocket(wsUri.value);
    websocket.binaryType = 'arraybuffer';

    websocket.onopen = function(evt) {
      onOpen(evt)
    };

    websocket.onclose = function(evt) {
       onClose(evt)
    };

    websocket.onmessage = function(evt) {
      onMessage(evt)
    };

    websocket.onerror = function(evt) {
      onError(evt)
    };
  }

  function doDisconnect() {
    // unsubscribe
    websocket.close()
  }

  function clearLog() {
    while (consoleLog.childNodes.length > 0) {
      consoleLog.removeChild(consoleLog.lastChild);
    }
  }

  function onOpen(evt) {
    writeToLog('<span style="color:black; font-size:75%; font-style:italic">CONNECTED</span>');
    setGuiConnected(true);
  }

  function onClose(evt) {
    writeToLog('<span style="color:black; font-size:75%; font-style:italic">DISCONNECTED</span>');
    setGuiConnected(false);
  }

  function onMessage(evt) {
    writeToLog(createMessage(extractBody(evt.data)), true);
  }

  function onError(evt) {
    writeToLog('<span style="color:red; font-size:75%; font-style:italic">ERROR</span><span style="color: red;"><pre>' + evt.data + '</pre></span>');
  }

  function doSub(evt) {
    var msg = modeatomCB.checked ? "GET /cxf/samples/logbrowser/logs/subscribe" : "GET /cxf/samples/logbrowser/logs/subscribe/alternate"; 
    writeToLog('<span style="color:green; font-size:75%; font-style:italic">SENT</span><span style="color: green;"><pre>' + createMessage(msg) + '</pre></span>');
    websocket.send(msg);
    setGuiSubscribed(true);
  }

  function doUnsub(evt) {
    var msg = "GET /cxf/samples/logbrowser/logs/unsubscribe/*";
    writeToLog('<span style="color:green; font-size:75%; font-style:italic">SENT</span><span style="color: green;"><pre>' + createMessage(msg) + '</pre></span>');
    websocket.send(msg);
    setGuiSubscribed(false);
  }

  function setGuiConnected(isConnected) {
    wsUri.disabled = isConnected;
    connectBut.disabled = isConnected;
    disconnectBut.disabled = !isConnected;
    subBut.disabled = !isConnected;
    unsubBut.disabled = true;
    modeatomCB.disabled = !isConnected;
  }

  function setGuiSubscribed(isSubscribed) {
    subBut.disabled = isSubscribed;
    unsubBut.disabled = !isSubscribed;
    modeatomCB.disabled = isSubscribed;
  }

  // painfully extracting the body by finding the \n delimiter
  function extractBody(data) {
    var ba = new Uint8Array(data);
    var aa = new Array(ba.length);
    var skipping = true;
    var maystart = true;
    for (i = 0; i < ba.length; i++) {
      if (skipping) {
	    if (ba[i] == 10) {
          if (maystart) {
            skipping = false;
          } else {
            maystart = true;
          }
        } else {
          if (ba[i] != 13) {
            maystart = false;
          }
          continue;
        }
      }
      aa[i] = String.fromCharCode(ba[i]);
    }
    return Array.prototype.slice.call(aa).join("");
  }

  function createMessage(data) {
    return data.replace(/&/g, '&amp;').replace(/</g, '&lt;');
  }

  function writeToLog(message, raw) {
    var pre = document.createElement(raw ? "code" : "p");
    pre.wordWrap = "break-word";
    pre.innerHTML = message;
    consoleLog.appendChild(pre);
    if (raw) {
       consoleLog.appendChild(document.createElement("br"));
       consoleLog.appendChild(document.createElement("br"));
    }
    while (consoleLog.childNodes.length > 100) {
      consoleLog.removeChild(consoleLog.firstChild);
    }
    
    consoleLog.scrollTop = consoleLog.scrollHeight;
  }

  window.addEventListener("load", init, false);

  </script>  
</body>
</html>
